package com.hontech.bv.conn.api

import com.hontech.bv.conn.Proto
import com.hontech.bv.conn.SerialPortMod
import com.hontech.bv.conn.serial.Uint16
import com.hontech.bv.conn.serial.Uint8

object Heat {

    suspend fun drawCtrl(type: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_DRAW_CTRL,
            Proto.ID_HEAT,
            arrayOf(Uint8(type))
        ).assert("抽水控制")
    }

    suspend fun stirBoxCtrl(type: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_STIR_BOX_CTRL,
            Proto.ID_HEAT,
            arrayOf(Uint8(type))
        ).assert("搅拌盒控制")
    }

    suspend fun waterMotorCtrl(type: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_WATER_MOTOR_CTRL,
            Proto.ID_HEAT,
            arrayOf(Uint8(type))
        ).assert("截热水电机")
    }

    suspend fun iceMotorCtrl(type: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_ICE_MOTOR_CTRL,
            Proto.ID_HEAT,
            arrayOf(Uint8(type))
        ).assert("截冰电机")
    }

    suspend fun partsCtrl(type: Int, value: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_PARTS_CTRL,
            Proto.ID_HEAT,
            arrayOf(Uint8(type), Uint8(value))
        ).assert("组件测试")
    }

    suspend fun init() {
        SerialPortMod.request(
            Proto.REQ_HEAT_INIT,
            Proto.ID_HEAT,
            arrayOf()
        ).assert("初始化")
    }

    suspend fun setFlow(value: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_SET_FLOW,
            Proto.ID_HEAT,
            arrayOf(Uint16(value))
        ).assert("设置流量计参数")
    }

    suspend fun getFlow(): Int {
        val frame = SerialPortMod.request(
            Proto.REQ_HEAT_GET_FLOW,
            Proto.ID_HEAT,
            arrayOf()
        )
        val info = Uint16()
        frame.parse(arrayOf(info))
        return info.value
    }

    suspend fun drawWater(ml: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_DRAW_WATER,
            Proto.ID_HEAT,
            arrayOf(Uint8(ml))
        ).assert("出水测试")
    }

    suspend fun preHeat(ctrl: Int, waterTemp: Int, steamTemp: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_PRE_HEAT,
            Proto.ID_HEAT,
            arrayOf(Uint8(ctrl), Uint8(waterTemp), Uint8(steamTemp))
        ).assert("预加热")
    }

    suspend fun dropSteam(duration: Int, temp: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_DROP_STEAM,
            Proto.ID_HEAT,
            arrayOf(Uint8(duration), Uint8(temp))
        ).assert("释放蒸汽")
    }

    suspend fun stirPowder(water: Int, temp: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_STIR_POWDER,
            Proto.ID_HEAT,
            arrayOf(Uint8(water), Uint8(temp))
        ).assert("搅拌茶粉")
    }

    suspend fun clean(sec: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_CLEAN,
            Proto.ID_HEAT,
            arrayOf(Uint8(sec))
        ).assert("清洗")
    }

    suspend fun preWater(ml: Int, temp: Int) {
        SerialPortMod.request(
            Proto.REQ_HEAT_PRE_WATER,
            Proto.ID_HEAT,
            arrayOf(Uint8(ml), Uint8(temp))
        ).assert("预放水")
    }
}